package com.iss.dao.imp;

import com.iss.dao.OrderDAO;
import com.iss.model.Customer;
import com.iss.model.Order;
import com.iss.util.DBUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class OrderDAOImp implements OrderDAO {
    
    private DBUtil dbUtil = new DBUtil();
    @Override
    public Boolean insert(Order order) {




        return null;
    }

    @Override
    public Boolean update(Order order) {

        String sql = "update `order` set is_closed=? where order_id=?";
        int num = dbUtil.update(sql,order.getClosed()?1:0,order.getOrderId());
        return num>0;
    }

    @Override
    public Boolean delete(Order order) {
        return null;
    }

    @Override
    public List<Order> query() {

      //  Order order =new Order();
        return null;
    }

    @Override
    public Boolean exists(Order order) {
        return null;
    }

    @Override
    public Order queryByRoomNO(String roomNO) {
        String sql = "select * from order_info where roomNO =?";
        ResultSet rs = dbUtil.query(sql, roomNO);
        try {
            rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return getOrder2(rs);
    }

    @Override
    public String getNewOrderNO() {

        String lastOrderNO="";
        String sql ="select order_no from `order`" +
                " where order_id = (select max(order_id) from `order`)";

        ResultSet rs = dbUtil.query(sql);
        try
        {
            while (rs.next()){
               lastOrderNO=rs.getString("order_no");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        int lastNum =Integer.parseInt(lastOrderNO.substring(lastOrderNO.length()-2)) ;
        return lastOrderNO.substring(0,lastOrderNO.length()-2)+(lastNum+1);
    }

    private Order getOrder2(ResultSet rs) {
        Order order = new Order();
        try {
            order.setOrderId(rs.getInt("order_id"));
            order.setOderNO(rs.getString("order_no"));
            Customer customer = new Customer();
            customer.setCustomerName(rs.getString("customer_name"));
            order.setCustomer(customer);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return order;
    }

}
